package com.flyway.controller.user;

import com.flyway.anno.SecretBody;
import com.flyway.anno.UserLoginToken;
import com.flyway.pojo.Users;
import com.flyway.service.UserService;
import com.flyway.utils.R;
import com.flyway.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: xupx
 * @Date: 2019/11/11
 */
@RestController
@Slf4j
//@SecretBody
@Api(value = "UserController", tags = {"用户登入全局校验"})
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ApiOperation(value = "登入")
    public R login(@RequestBody Users users) {
        Users usersDb = userService.findUserByUserName(users.getUsername());
        //密码MD5校验等处理
        String token = TokenUtils.getToken(usersDb);
        R r = R.ok();
        r.put("token", token);
        r.put("userInfo", usersDb);
        return r;
    }

    @UserLoginToken
    @PostMapping(value = "/info", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ApiOperation(value = "获取用户信息")
    public R info(@RequestBody Users users) {
        R r = R.ok();
        Users userById = userService.findUserById(users.getUserId());
        r.put("users", userById);
        return r;
    }
}
